package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.database.DataSetObservable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class bz extends DataSetObservable {
    private static final Map<String, bz> ʻ = new HashMap();
    static final String ˊ = bz.class.getSimpleName();
    private static final Object ॱ = new Object();
    private final List<c> ʼ;
    private final List<a> ʽ;
    private boolean ˊॱ;
    final Context ˋ;
    private boolean ˋॱ;
    boolean ˎ;
    final String ˏ;
    private boolean ˏॱ;
    private d ͺ;
    private int ॱˊ;
    private final Object ॱॱ;
    private e ॱᐝ;
    private Intent ᐝ;

    public static final class a {
        public final long ˋ;
        public final float ˎ;
        public final ComponentName ˏ;

        public a(String str, long j, float f) {
            this(ComponentName.unflattenFromString(str), j, f);
        }

        public a(ComponentName componentName, long j, float f) {
            this.ˏ = componentName;
            this.ˋ = j;
            this.ˎ = f;
        }

        public int hashCode() {
            return (((((this.ˏ == null ? 0 : this.ˏ.hashCode()) + 31) * 31) + ((int) (this.ˋ ^ (this.ˋ >>> 32)))) * 31) + Float.floatToIntBits(this.ˎ);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            a aVar = (a) obj;
            if (this.ˏ == null) {
                if (aVar.ˏ != null) {
                    return false;
                }
            } else if (!this.ˏ.equals(aVar.ˏ)) {
                return false;
            }
            if (this.ˋ != aVar.ˋ) {
                return false;
            }
            if (Float.floatToIntBits(this.ˎ) != Float.floatToIntBits(aVar.ˎ)) {
                return false;
            }
            return true;
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[");
            stringBuilder.append("; activity:").append(this.ˏ);
            stringBuilder.append("; time:").append(this.ˋ);
            stringBuilder.append("; weight:").append(new BigDecimal((double) this.ˎ));
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
    }

    final class b extends AsyncTask<Object, Void, Void> {
        final /* synthetic */ bz ˏ;

        public /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˋ(objArr);
        }

        b(bz bzVar) {
            this.ˏ = bzVar;
        }

        public Void ˋ(Object... objArr) {
            int i = 0;
            List list = (List) objArr[0];
            String str = (String) objArr[1];
            try {
                OutputStream openFileOutput = this.ˏ.ˋ.openFileOutput(str, 0);
                XmlSerializer newSerializer = Xml.newSerializer();
                try {
                    newSerializer.setOutput(openFileOutput, null);
                    newSerializer.startDocument("UTF-8", Boolean.valueOf(true));
                    newSerializer.startTag(null, "historical-records");
                    int size = list.size();
                    while (i < size) {
                        a aVar = (a) list.remove(0);
                        newSerializer.startTag(null, "historical-record");
                        newSerializer.attribute(null, "activity", aVar.ˏ.flattenToString());
                        newSerializer.attribute(null, "time", String.valueOf(aVar.ˋ));
                        newSerializer.attribute(null, "weight", String.valueOf(aVar.ˎ));
                        newSerializer.endTag(null, "historical-record");
                        i++;
                    }
                    newSerializer.endTag(null, "historical-records");
                    newSerializer.endDocument();
                    this.ˏ.ˎ = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException e) {
                        }
                    }
                } catch (Throwable e2) {
                    Log.e(bz.ˊ, "Error writing historical record file: " + this.ˏ.ˏ, e2);
                    this.ˏ.ˎ = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException e3) {
                        }
                    }
                } catch (Throwable e22) {
                    Log.e(bz.ˊ, "Error writing historical record file: " + this.ˏ.ˏ, e22);
                    this.ˏ.ˎ = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException e4) {
                        }
                    }
                } catch (Throwable e222) {
                    Log.e(bz.ˊ, "Error writing historical record file: " + this.ˏ.ˏ, e222);
                    this.ˏ.ˎ = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException e5) {
                        }
                    }
                } catch (Throwable th) {
                    this.ˏ.ˎ = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException e6) {
                        }
                    }
                }
            } catch (Throwable e2222) {
                Log.e(bz.ˊ, "Error writing historical record file: " + str, e2222);
            }
            return null;
        }
    }

    public static final class c implements Comparable<c> {
        public float ˊ;
        public final ResolveInfo ˋ;

        public /* synthetic */ int compareTo(Object obj) {
            return ॱ((c) obj);
        }

        public c(ResolveInfo resolveInfo) {
            this.ˋ = resolveInfo;
        }

        public int hashCode() {
            return Float.floatToIntBits(this.ˊ) + 31;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            if (Float.floatToIntBits(this.ˊ) != Float.floatToIntBits(((c) obj).ˊ)) {
                return false;
            }
            return true;
        }

        public int ॱ(c cVar) {
            return Float.floatToIntBits(cVar.ˊ) - Float.floatToIntBits(this.ˊ);
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[");
            stringBuilder.append("resolveInfo:").append(this.ˋ.toString());
            stringBuilder.append("; weight:").append(new BigDecimal((double) this.ˊ));
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
    }

    public interface d {
        void ˎ(Intent intent, List<c> list, List<a> list2);
    }

    public interface e {
        boolean ˋ(bz bzVar, Intent intent);
    }

    public int ˏ() {
        int size;
        synchronized (this.ॱॱ) {
            ˎ();
            size = this.ʼ.size();
        }
        return size;
    }

    public ResolveInfo ˊ(int i) {
        ResolveInfo resolveInfo;
        synchronized (this.ॱॱ) {
            ˎ();
            resolveInfo = ((c) this.ʼ.get(i)).ˋ;
        }
        return resolveInfo;
    }

    public int ˊ(ResolveInfo resolveInfo) {
        synchronized (this.ॱॱ) {
            ˎ();
            List list = this.ʼ;
            int size = list.size();
            for (int i = 0; i < size; i++) {
                if (((c) list.get(i)).ˋ == resolveInfo) {
                    return i;
                }
            }
            return -1;
        }
    }

    public Intent ॱ(int i) {
        synchronized (this.ॱॱ) {
            if (this.ᐝ == null) {
                return null;
            }
            ˎ();
            c cVar = (c) this.ʼ.get(i);
            ComponentName componentName = new ComponentName(cVar.ˋ.activityInfo.packageName, cVar.ˋ.activityInfo.name);
            Intent intent = new Intent(this.ᐝ);
            intent.setComponent(componentName);
            if (this.ॱᐝ != null) {
                if (this.ॱᐝ.ˋ(this, new Intent(intent))) {
                    return null;
                }
            }
            ˋ(new a(componentName, System.currentTimeMillis(), 1.0f));
            return intent;
        }
    }

    public ResolveInfo ॱ() {
        synchronized (this.ॱॱ) {
            ˎ();
            if (this.ʼ.isEmpty()) {
                return null;
            }
            ResolveInfo resolveInfo = ((c) this.ʼ.get(0)).ˋ;
            return resolveInfo;
        }
    }

    public void ˎ(int i) {
        synchronized (this.ॱॱ) {
            float f;
            ˎ();
            c cVar = (c) this.ʼ.get(i);
            c cVar2 = (c) this.ʼ.get(0);
            if (cVar2 != null) {
                f = (cVar2.ˊ - cVar.ˊ) + 5.0f;
            } else {
                f = 1.0f;
            }
            ˋ(new a(new ComponentName(cVar.ˋ.activityInfo.packageName, cVar.ˋ.activityInfo.name), System.currentTimeMillis(), f));
        }
    }

    private void ˊ() {
        if (!this.ˋॱ) {
            throw new IllegalStateException("No preceding call to #readHistoricalData");
        } else if (this.ˊॱ) {
            this.ˊॱ = false;
            if (!TextUtils.isEmpty(this.ˏ)) {
                new b(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Object[]{new ArrayList(this.ʽ), this.ˏ});
            }
        }
    }

    public int ˋ() {
        int size;
        synchronized (this.ॱॱ) {
            ˎ();
            size = this.ʽ.size();
        }
        return size;
    }

    private void ˎ() {
        int ᐝ = ᐝ() | ʼ();
        ʻ();
        if (ᐝ != 0) {
            ॱॱ();
            notifyChanged();
        }
    }

    private boolean ॱॱ() {
        if (this.ͺ == null || this.ᐝ == null || this.ʼ.isEmpty() || this.ʽ.isEmpty()) {
            return false;
        }
        this.ͺ.ˎ(this.ᐝ, this.ʼ, Collections.unmodifiableList(this.ʽ));
        return true;
    }

    private boolean ᐝ() {
        if (!this.ˏॱ || this.ᐝ == null) {
            return false;
        }
        this.ˏॱ = false;
        this.ʼ.clear();
        List queryIntentActivities = this.ˋ.getPackageManager().queryIntentActivities(this.ᐝ, 0);
        int size = queryIntentActivities.size();
        for (int i = 0; i < size; i++) {
            this.ʼ.add(new c((ResolveInfo) queryIntentActivities.get(i)));
        }
        return true;
    }

    private boolean ʼ() {
        if (!this.ˎ || !this.ˊॱ || TextUtils.isEmpty(this.ˏ)) {
            return false;
        }
        this.ˎ = false;
        this.ˋॱ = true;
        ʽ();
        return true;
    }

    private boolean ˋ(a aVar) {
        boolean add = this.ʽ.add(aVar);
        if (add) {
            this.ˊॱ = true;
            ʻ();
            ˊ();
            ॱॱ();
            notifyChanged();
        }
        return add;
    }

    private void ʻ() {
        int size = this.ʽ.size() - this.ॱˊ;
        if (size > 0) {
            this.ˊॱ = true;
            for (int i = 0; i < size; i++) {
                a aVar = (a) this.ʽ.remove(0);
            }
        }
    }

    private void ʽ() {
        try {
            InputStream openFileInput = this.ˋ.openFileInput(this.ˏ);
            try {
                XmlPullParser newPullParser = Xml.newPullParser();
                newPullParser.setInput(openFileInput, "UTF-8");
                int i = 0;
                while (i != 1 && i != 2) {
                    i = newPullParser.next();
                }
                if ("historical-records".equals(newPullParser.getName())) {
                    List list = this.ʽ;
                    list.clear();
                    while (true) {
                        int next = newPullParser.next();
                        if (next == 1) {
                            break;
                        } else if (!(next == 3 || next == 4)) {
                            if ("historical-record".equals(newPullParser.getName())) {
                                list.add(new a(newPullParser.getAttributeValue(null, "activity"), Long.parseLong(newPullParser.getAttributeValue(null, "time")), Float.parseFloat(newPullParser.getAttributeValue(null, "weight"))));
                            } else {
                                throw new XmlPullParserException("Share records file not well-formed.");
                            }
                        }
                    }
                    if (openFileInput != null) {
                        try {
                            openFileInput.close();
                            return;
                        } catch (IOException e) {
                            return;
                        }
                    }
                    return;
                }
                throw new XmlPullParserException("Share records file does not start with historical-records tag.");
            } catch (Throwable e2) {
                Log.e(ˊ, "Error reading historical recrod file: " + this.ˏ, e2);
                if (openFileInput != null) {
                    try {
                        openFileInput.close();
                    } catch (IOException e3) {
                    }
                }
            } catch (Throwable e22) {
                Log.e(ˊ, "Error reading historical recrod file: " + this.ˏ, e22);
                if (openFileInput != null) {
                    try {
                        openFileInput.close();
                    } catch (IOException e4) {
                    }
                }
            } catch (Throwable th) {
                if (openFileInput != null) {
                    try {
                        openFileInput.close();
                    } catch (IOException e5) {
                    }
                }
            }
        } catch (FileNotFoundException e6) {
        }
    }
}
